package portlet;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;
import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import portlet.resource.quyetdinhcongboBundle;

public class quyetdinhcongbo extends ADFBridgePortlet {
    private Object langid;
    private Object urlquyetdinh;
    private Object tukhoa;
    //Label
    private String labelNoData;
    private String labelNoAccess;
    private String labelTieude;
    private String labelTrangdau;
    private String labelTrangtruoc;
    private String labelTrangsau;
    private String labelTrangcuoi;
    private String labelTukhoa;
    private String labelTimkiem;
    private String labelDanhsach;
    private String labelBothutuc;
    private String labelPhamvithutuc;
    //Paging
    private boolean paging = false;
    private int pageNumber;
    private static final int RANGE_SIZE = 10;
    public String getViewRender() {
        getQuyetdinhCongbo();
        return null;
    }
    
    private void getQuyetdinhCongbo(){
        getLangid();
        DCIteratorBinding qdcbIterator =
            Utils.getIteratorBinding("TthcQdcbView1Iterator");
        ViewObject vo = qdcbIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("qdcbViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        if (tukhoa != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("Tieude");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_CONTAINS);
            rowViewCriteriaItem2.setValue(tukhoa.toString().toUpperCase());
            rowViewCriteriaItem2.setUpperColumns(true);
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("qdcbViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        checkPaging();
    }
    
    public void timkiem() {
        getQuyetdinhCongbo();
    }

    public Object getLangid() {
        try {
            langid = Utils.getPortalLocale().getLanguage();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return langid;
    }

    public Object getUrlquyetdinh() {
        try {
            urlquyetdinh = Utils.getPortletRequest().getParameter("urlquyetdinh");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlquyetdinh;
    }

    public String goLastPage() {
        Utils.goLastPage(RANGE_SIZE, "TthcQdcbView1");
        return null;
    }

    public boolean isLastEnabled() {
        return Utils.isLastEnabled(RANGE_SIZE, "TthcQdcbView1");
    }

    public boolean isFirstEnabled() {
        return Utils.isFirstEnabled(RANGE_SIZE, "TthcQdcbView1");
    }

    public void setPageNumber(int pageNumber) {
        JUCtrlRangeBinding qdcbView = this.getQdcbView();
        if (pageNumber >
            Math.max(1, (qdcbView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumber =
                    (int)Math.max(1, (qdcbView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumber = pageNumber;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    private JUCtrlRangeBinding getQdcbView() {
        return Utils.getViewObject("TthcQdcbView1");
    }

    private void checkPaging() {
        long lastPageNum =
            Math.max(1, (getQdcbView().getEstimatedRowCount() - 1) /
                     RANGE_SIZE + 1);
        if (lastPageNum > 1) {
            setPaging(true);
        }
    }

    public void setPaging(boolean paging) {
        this.paging = paging;
    }

    public boolean isPaging() {
        return paging;
    }

    public String getLabelNoData() {
        labelNoData = Utils.getResource("NO_DATA");
        return labelNoData;
    }

    public String getLabelNoAccess() {
        labelNoAccess = Utils.getResource("ACCESS_DENIED");
        return labelNoAccess;
    }

    public String getLabelTieude() {
        labelTieude = Utils.getResource("QUYET_DINH_CONG_BO");
        return labelTieude;
    }

    public String getLabelTrangdau() {
        labelTrangdau = Utils.getResource("TRANG_DAU");
        return labelTrangdau;
    }

    public String getLabelTrangtruoc() {
        labelTrangtruoc = Utils.getResource("TRANG_TRUOC");
        return labelTrangtruoc;
    }

    public String getLabelTrangsau() {
        labelTrangsau = Utils.getResource("TRANG_SAU");
        return labelTrangsau;
    }

    public String getLabelTrangcuoi() {
        labelTrangcuoi = Utils.getResource("TRANG_CUOI");
        return labelTrangcuoi;
    }

    public void setTukhoa(Object tukhoa) {
        this.tukhoa = tukhoa;
    }

    public Object getTukhoa() {
        if (tukhoa == null) {
            tukhoa = "";
        }
        return tukhoa;
    }

    public String getLabelTukhoa() {
        labelTukhoa = Utils.getResource("TU_KHOA");
        return labelTukhoa;
    }

    public String getLabelTimkiem() {
        labelTimkiem = Utils.getResource("TIM_KIEM");
        return labelTimkiem;
    }

    public String getLabelDanhsach() {
        labelDanhsach = Utils.getResource("DANH_SACH_QUYET_DINH");
        return labelDanhsach;
    }

    public String getLabelBothutuc() {
        labelBothutuc = Utils.getResource("BO_THU_TUC_HANH_CHINH");
        return labelBothutuc;
    }

    public String getLabelPhamvithutuc() {
        labelPhamvithutuc = Utils.getResource("PHAM_VI_THU_TUC");
        return labelPhamvithutuc;
    }
}
